package com.hyt.it.ogt.ykcj.service.grade.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hyt.core.constant.CommonConstant;
import com.hyt.core.util.UUIDUtils;
import com.hyt.it.ogt.ykcj.common.constant.Constants;
import com.hyt.it.ogt.ykcj.common.enums.CommonEnums;
import com.hyt.it.ogt.ykcj.common.enums.ExamFormEnums;
import com.hyt.it.ogt.ykcj.common.enums.PublishTypeEnums;
import com.hyt.it.ogt.ykcj.common.enums.ShowTypeEnums;
import com.hyt.it.ogt.ykcj.common.exception.CustomException;
import com.hyt.it.ogt.ykcj.common.utils.DateUtils;
import com.hyt.it.ogt.ykcj.domain.exam.ExamInfo;
import com.hyt.it.ogt.ykcj.domain.exam.ExamNameConfig;
import com.hyt.it.ogt.ykcj.domain.exam.ExamPublishCertificateInfo;
import com.hyt.it.ogt.ykcj.domain.exchange.ExamineeExamInfo;
import com.hyt.it.ogt.ykcj.domain.grade.ExamPublishInfo;
import com.hyt.it.ogt.ykcj.domain.grade.ExamPublishSubjectInfo;
import com.hyt.it.ogt.ykcj.domain.grade.ExamineeExamPaperInfo;
import com.hyt.it.ogt.ykcj.domain.param.ScoreLineItem;
import com.hyt.it.ogt.ykcj.domain.vo.*;
import com.hyt.it.ogt.ykcj.domain.vo.exchange.CandidateAnswersVO;
import com.hyt.it.ogt.ykcj.domain.vo.exchange.CandidatePaperDataVO;
import com.hyt.it.ogt.ykcj.domain.zs.ZsInfo;
import com.hyt.it.ogt.ykcj.domain.zs.vo.ZsInfoVO;
import com.hyt.it.ogt.ykcj.mapper.exam.EnterpriseExamScoreLineSettingSubMapper;
import com.hyt.it.ogt.ykcj.mapper.exam.ExamInfoMapper;
import com.hyt.it.ogt.ykcj.mapper.exam.ExamNameConfigMapper;
import com.hyt.it.ogt.ykcj.mapper.exam.ExamSubjectInfoMapper;
import com.hyt.it.ogt.ykcj.mapper.exchange.ExamineeExamInfoMapper;
import com.hyt.it.ogt.ykcj.mapper.grade.ExamPublishInfoMapper;
import com.hyt.it.ogt.ykcj.mapper.grade.ExamPublishSubjectInfoMapper;
import com.hyt.it.ogt.ykcj.mapper.grade.ExamineeExamPaperInfoMapper;
import com.hyt.it.ogt.ykcj.service.exam.IExamPublishCertificateInfoService;
import com.hyt.it.ogt.ykcj.service.grade.IEnterpriseExamineeCertificateRelationService;
import com.hyt.it.ogt.ykcj.service.grade.IEnterpriseExamineePublishInfoService;
import com.hyt.it.ogt.ykcj.service.zs.IZsInfoService;
import com.hyt.loginfo.model.User;
import com.hyt.loginfo.service.IBaseLoginService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;


/**
 * 企业招聘考生复核信息Service业务层处理
 *
 * @author liying
 * @date 2021-11-19
 */
@Slf4j
@Service
public class EnterpriseExamineePublishInfoServiceImpl implements IEnterpriseExamineePublishInfoService {

    @Autowired
    private ExamPublishInfoMapper examPublishInfoMapper;

    @Autowired
    private ExamPublishSubjectInfoMapper examPublishSubjectInfoMapper;

    @Autowired
    private IBaseLoginService iBaseLoginService;

    @Autowired
    private ExamSubjectInfoMapper examSubjectInfoMapper;

    @Autowired
    private ExamInfoMapper examInfoMapper;

    @Autowired
    private EnterpriseExamScoreLineSettingSubMapper enterpriseExamScoreLineSettingSubMapper;

    @Autowired
    private IZsInfoService zsInfoService;

    @Autowired
    private ExamineeExamPaperInfoMapper examineeExamPaperInfoMapper;

    @Autowired
    private ExamNameConfigMapper examNameConfigMapper;

    @Autowired
    private ExamineeExamInfoMapper examineeExamInfoMapper;
    @Resource
    IExamPublishCertificateInfoService examPublishCertificateInfoService;
    @Resource
    IEnterpriseExamineeCertificateRelationService enterpriseExamineeCertificateRelationService;

    @Override
    public ExamPublishVo selectExamPublishInfoById(String examId) {
        ExamInfo examInfo = examInfoMapper.selectExamInfoById(examId);
        if (null == examInfo) {
            return null;
        }

        LambdaQueryWrapper<ExamPublishInfo> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ExamPublishInfo::getExamId, examId);
        List<ExamPublishInfo> examPublishInfos = examPublishInfoMapper.selectList(wrapper);
        if (CollectionUtils.isNotEmpty(examPublishInfos)) {
            return bulidExisExamPublishVo(examInfo, examPublishInfos);
        } else {
            return bulidInitExamPublishVo(examInfo);
        }
    }

    /**
     * 初始化考试发布信息实体
     *
     * @param examInfo
     * @return
     */
    private ExamPublishVo bulidInitExamPublishVo(ExamInfo examInfo) {
        ExamPublishVo examPublishVo = new ExamPublishVo();
        examPublishVo.setExamId(examInfo.getId());
        examPublishVo.setExamForm(examInfo.getExamForm());
        //合并科目需按照普通考试处理
        if (null != examInfo.getMergeType() &&
                CommonEnums.EXAM_MERGE_TYPE_1.getCode().equals(String.valueOf(examInfo.getMergeType()))) {
            examPublishVo.setExamForm(ExamFormEnums.FORM_EXAM.getCode());
        }
        List<ExamInfoVo> examInfoVos = getExamInfoVos(examInfo.getId());
        //成绩显示
        examPublishVo.setExamResultVo(initExamResultVo(examInfo.getId(), examInfoVos));
        //查看试卷
        examPublishVo.setExamPaperVo(initExamPaperVo(examInfo, examInfoVos));
        //成绩复核申请
        examPublishVo.setExamReviewVo(initExamReviewVo(examInfo.getId()));
        //证书发布
        examPublishVo.setExamPublishZsInfoVo(initExamPublishZsInfoVo(examInfo.getId()));

        return examPublishVo;
    }

    /**
     * 初始化成绩发布证书信息
     *
     * @param examId
     * @return
     */
    private ExamPublishInfoVo initExamPublishZsInfoVo(String examId) {
        // 查询考试是否关联证书
        ZsInfoVO zsInfoVO = zsInfoService.getZsInfoByPublishTypeToExam(examId);
        ExamPublishInfoVo examPublishZsInfoVo = new ExamPublishInfoVo();
        examPublishZsInfoVo.setExamId(examId);
        examPublishZsInfoVo.setShowPublishTypeResult(Integer.valueOf(CommonEnums.CHECK_STATUS_0.getCode()));
        examPublishZsInfoVo.setPublishType(PublishTypeEnums.ZS_DOWNLOAD_TYPE_3.getCode());
        if (null == zsInfoVO) {
            examPublishZsInfoVo.setRelationZs(Boolean.FALSE);
        } else {
            examPublishZsInfoVo.setRelationZs(Boolean.TRUE);

            List<ZsInfo> zsInfos = zsInfoService.listByExamId(examId);
            List<ExamPublishCertificateInfoVO> certificateInfoVOS = zsInfos.stream()
                    .map(this::assemblePublishCertificateInfoVO)
                    .collect(Collectors.toList());
            examPublishZsInfoVo.setExamPublishCertificateInfoVOS(certificateInfoVOS);
        }
        return examPublishZsInfoVo;
    }

    private ExamPublishInfoVo initExamReviewVo(String examId) {
        ExamPublishInfoVo examReviewVo = new ExamPublishInfoVo();
        examReviewVo.setExamId(examId);
        examReviewVo.setShowTypes(Collections.EMPTY_LIST);
        examReviewVo.setShowPublishTypeResult(Integer.valueOf(CommonEnums.CHECK_STATUS_0.getCode()));
        examReviewVo.setPublishType(PublishTypeEnums.PUBLISH_TYPE_2.getCode());
        examReviewVo.setSeeType(null);
        examReviewVo.setEndDay(null);
        examReviewVo.setStartDay(null);
        examReviewVo.setExamInfoVos(null);
        examReviewVo.setSubjectInfoVos(null);
        examReviewVo.setEaxmPublishPaperVOs(null);
        return examReviewVo;
    }

    private ExamPublishInfoVo initExamPaperVo(ExamInfo examInfo, List<ExamInfoVo> examInfoVos) {
        ExamPublishInfoVo examPaperVo = new ExamPublishInfoVo();
        examPaperVo.setExamId(examInfo.getId());
        examPaperVo.setShowTypes(Collections.EMPTY_LIST);
        examPaperVo.setShowPublishTypeResult(Integer.valueOf(CommonEnums.CHECK_STATUS_0.getCode()));
        examPaperVo.setPublishType(PublishTypeEnums.PUBLISH_TYPE_1.getCode());
        examPaperVo.setSeeType(null);
        examPaperVo.setEndDay(null);
        examPaperVo.setStartDay(null);
        examPaperVo.setExamInfoVos(null);
        examPaperVo.setSubjectInfoVos(null);

        //普通考试
        if (CollectionUtils.isEmpty(examInfoVos)) {
            List<EaxmPublishPaperVO> eaxmPublishPaperVOs = new ArrayList<>();
            ExamNameConfig examNameConfig = examNameConfigMapper.selectExamNameConfigById(examInfo.getExamNameConfigId());
            EaxmPublishPaperVO eaxmPublishPaperVo = new EaxmPublishPaperVO();
            eaxmPublishPaperVo.setExamName(examNameConfig.getName());
            eaxmPublishPaperVo.setExamId(examInfo.getId());
            QueryWrapper<ExamineeExamInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(ExamineeExamInfo::getExamId, examInfo.getId()).groupBy(ExamineeExamInfo::getPaperId);
            List<ExamineeExamInfo> examineeExamInfos = examineeExamInfoMapper.selectList(queryWrapper);
            if (CollectionUtils.isEmpty(examineeExamInfos)) {
                return examPaperVo;
            }
            //A.B卷需要单独处理
            if (examineeExamInfos.size() > 1) {
                List<PaperPublishVO> paperVOs = new ArrayList<>(examineeExamInfos.size());
                for (ExamineeExamInfo examineeExamInfo : examineeExamInfos) {
                    if (StringUtils.isEmpty(examineeExamInfo.getExamineeExamInfo())) {
                        continue;
                    }
                    CandidateAnswersVO candidateAnswersVO = JSON.parseObject(examineeExamInfo.getExamineeExamInfo(), CandidateAnswersVO.class);
                    if (CollectionUtils.isEmpty(candidateAnswersVO.getCandidatePaperDataList())) {
                        continue;
                    }

                    for (CandidatePaperDataVO paperDataVO : candidateAnswersVO.getCandidatePaperDataList()) {
                        paperVOs.add(buildPaperPublishVO(paperDataVO));
                    }


                }
                eaxmPublishPaperVo.setPaperVOs(paperVOs);
                eaxmPublishPaperVOs.add(eaxmPublishPaperVo);
            } else {
                if (StringUtils.isEmpty(examineeExamInfos.get(0).getExamineeExamInfo())) {
                    return examPaperVo;
                }
                CandidateAnswersVO candidateAnswersVO = JSON.parseObject(examineeExamInfos.get(0).getExamineeExamInfo(), CandidateAnswersVO.class);
                if (CollectionUtils.isEmpty(candidateAnswersVO.getCandidatePaperDataList())) {
                    return examPaperVo;
                }
                List<PaperPublishVO> paperVOs = new ArrayList<>();
                for (CandidatePaperDataVO paperDataVO : candidateAnswersVO.getCandidatePaperDataList()) {
                    paperVOs.add(buildPaperPublishVO(paperDataVO));
                }
                eaxmPublishPaperVo.setPaperVOs(paperVOs);

                eaxmPublishPaperVOs.add(eaxmPublishPaperVo);

            }
            examPaperVo.setEaxmPublishPaperVOs(eaxmPublishPaperVOs);
            //考试组
        } else {
            List<EaxmPublishPaperVO> eaxmPublishPaperVOs = new ArrayList<>();
            for (ExamInfoVo examInfoVo : examInfoVos) {
                EaxmPublishPaperVO eaxmPublishPaperVo = new EaxmPublishPaperVO();
                eaxmPublishPaperVo.setExamName(examInfoVo.getExamNameConfigName());
                eaxmPublishPaperVo.setExamId(examInfoVo.getId());
                QueryWrapper<ExamineeExamInfo> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(ExamineeExamInfo::getExamId, examInfoVo.getId()).groupBy(ExamineeExamInfo::getPaperId);
                List<ExamineeExamInfo> examineeExamInfos = examineeExamInfoMapper.selectList(queryWrapper);
                if (CollectionUtils.isEmpty(examineeExamInfos)) {
                    return examPaperVo;
                }
                if (examineeExamInfos.size() > 1) {
                    List<PaperPublishVO> paperVOs = new ArrayList<>(examineeExamInfos.size());
                    for (ExamineeExamInfo examineeExamInfo : examineeExamInfos) {
                        if (StringUtils.isEmpty(examineeExamInfo.getExamineeExamInfo())) {
                            continue;
                        }
                        CandidateAnswersVO candidateAnswersVO = JSON.parseObject(examineeExamInfo.getExamineeExamInfo(), CandidateAnswersVO.class);
                        if (CollectionUtils.isEmpty(candidateAnswersVO.getCandidatePaperDataList())) {
                            continue;
                        }

                        for (CandidatePaperDataVO paperDataVO : candidateAnswersVO.getCandidatePaperDataList()) {
                            paperVOs.add(buildPaperPublishVO(paperDataVO));
                        }


                    }
                    eaxmPublishPaperVo.getPaperVOs().addAll(paperVOs);
                    eaxmPublishPaperVOs.add(eaxmPublishPaperVo);
                } else {
                    if (StringUtils.isEmpty(examineeExamInfos.get(0).getExamineeExamInfo())) {
                        continue;
                    }
                    CandidateAnswersVO candidateAnswersVO = JSON.parseObject(examineeExamInfos.get(0).getExamineeExamInfo(), CandidateAnswersVO.class);
                    if (CollectionUtils.isEmpty(candidateAnswersVO.getCandidatePaperDataList())) {
                        continue;
                    }
                    List<PaperPublishVO> paperVOs = new ArrayList<>(candidateAnswersVO.getCandidatePaperDataList().size());
                    for (CandidatePaperDataVO paperDataVO : candidateAnswersVO.getCandidatePaperDataList()) {
                        paperVOs.add(buildPaperPublishVO(paperDataVO));
                    }
                    eaxmPublishPaperVo.setPaperVOs(paperVOs);
                    eaxmPublishPaperVOs.add(eaxmPublishPaperVo);
                }

            }
            examPaperVo.setEaxmPublishPaperVOs(eaxmPublishPaperVOs);
        }

        return examPaperVo;
    }

    private ExamPublishInfoVo initExamResultVo(String examId, List<ExamInfoVo> examInfoVos) {
        ExamPublishInfoVo examResultVo = new ExamPublishInfoVo();
        examResultVo.setExamId(examId);
        examResultVo.setShowPublishTypeResult(Integer.valueOf(CommonEnums.CHECK_STATUS_0.getCode()));
        if (CollectionUtils.isEmpty(examInfoVos)) {
            examResultVo.setShowTypes(Collections.EMPTY_LIST);
            //查考试下的科目
            List<ExamSubjectVo> examSubjectVos = examSubjectInfoMapper.selecSubjectList(examId);
            List<ExamPublishSubjectInfoVo> subjectInfoVos = null;
            if (CollectionUtils.isNotEmpty(examSubjectVos)) {
                subjectInfoVos = new ArrayList<>(examSubjectVos.size());
                for (ExamSubjectVo examSubjectVo : examSubjectVos) {
                    subjectInfoVos.add(buildExamPublishSubjectInfoVo(examSubjectVo));
                }

            }
            examResultVo.setSubjectInfoVos(subjectInfoVos);
            examResultVo.setExamInfoVos(null);
        } else {
            examResultVo.setShowTypes(Collections.EMPTY_LIST);
            List<ExamPublishExamnfoVo> examPublishExamnfoVos = new ArrayList<>(examInfoVos.size());
            for (ExamInfoVo examInfoVo : examInfoVos) {
                ExamPublishExamnfoVo examPublishExamnfoVo = new ExamPublishExamnfoVo();
                List<ExamSubjectVo> examSubjectVos = examSubjectInfoMapper.selecSubjectList(examInfoVo.getId());
                List<ExamPublishSubjectInfoVo> examPublishSubjectInfoVos = null;
                if (CollectionUtils.isNotEmpty(examSubjectVos)) {
                    examPublishSubjectInfoVos = new ArrayList<>(examSubjectVos.size());
                    for (ExamSubjectVo examSubjectVo : examSubjectVos) {
                        examPublishSubjectInfoVos.add(buildExamPublishSubjectInfoVo(examSubjectVo));
                    }
                }
                examPublishExamnfoVo.setExamPublishSubjectInfoVos(examPublishSubjectInfoVos);
                examPublishExamnfoVo.setShowExamResult(Integer.valueOf(CommonEnums.CHECK_STATUS_0.getCode()));
                examPublishExamnfoVo.setShowExamScore(Integer.valueOf(CommonEnums.CHECK_STATUS_0.getCode()));
                examPublishExamnfoVo.setRelationExamName(examInfoVo.getExamNameConfigName());
                examPublishExamnfoVo.setRelationExamId(examInfoVo.getId());
                examPublishExamnfoVo.setExamPublishId(null);
                examPublishExamnfoVos.add(examPublishExamnfoVo);
            }
            examResultVo.setExamInfoVos(examPublishExamnfoVos);
            examResultVo.setSubjectInfoVos(null);
        }
        examResultVo.setPublishType(PublishTypeEnums.PUBLISH_TYPE_0.getCode());
        examResultVo.setSeeType(null);
        examResultVo.setEndDay(null);
        examResultVo.setEndDay(null);
        examResultVo.setEaxmPublishPaperVOs(null);
        return examResultVo;
    }


    private PaperPublishVO buildPaperPublishVO(CandidatePaperDataVO paperDataVO) {
        PaperPublishVO paperPublishVO = new PaperPublishVO();
        paperPublishVO.setShowPaperResult(Integer.valueOf(CommonEnums.CHECK_STATUS_0.getCode()));
        paperPublishVO.setPaperId(paperDataVO.getPaperId());
        paperPublishVO.setPaperName(paperDataVO.getPaperName());
        return paperPublishVO;
    }

    private ExamPublishSubjectInfoVo buildExamPublishSubjectInfoVo(ExamSubjectVo examSubjectVo) {
        ExamPublishSubjectInfoVo subjectInfoVo = new ExamPublishSubjectInfoVo();
        subjectInfoVo.setExamPublishId(null);
        subjectInfoVo.setShowSubjectResult(Integer.valueOf(CommonEnums.CHECK_STATUS_0.getCode()));
        subjectInfoVo.setShowSubjectScore(Integer.valueOf(CommonEnums.CHECK_STATUS_0.getCode()));
        subjectInfoVo.setSubjectId(examSubjectVo.getSubjectId());
        subjectInfoVo.setSubjectName(examSubjectVo.getSubjectName());
        return subjectInfoVo;
    }

    /**
     * 存在发布信息
     *
     * @param examInfo
     * @param examPublishInfos
     * @return
     */
    private ExamPublishVo bulidExisExamPublishVo(ExamInfo examInfo, List<ExamPublishInfo> examPublishInfos) {
        ExamPublishVo examPublishVo = new ExamPublishVo();
        examPublishVo.setExamId(examInfo.getId());
        examPublishVo.setExamForm(examInfo.getExamForm());
        // 合并科目需按照普通考试处理
        if (null != examInfo.getMergeType() && CommonEnums.EXAM_MERGE_TYPE_1.getCode().equals(String.valueOf(examInfo.getMergeType()))) {
            examPublishVo.setExamForm(ExamFormEnums.FORM_EXAM.getCode());
        }
        for (ExamPublishInfo examPublishInfo : examPublishInfos) {
            ExamPublishInfoVo examResultVo = new ExamPublishInfoVo();
            BeanUtils.copyProperties(examPublishInfo, examResultVo);
            if (StringUtils.isNotEmpty(examPublishInfo.getShowType())) {
                examResultVo.setShowTypes(Arrays.asList(examPublishInfo.getShowType().split(",")));
            }
            if (PublishTypeEnums.PUBLISH_TYPE_0.getCode().equals(examPublishInfo.getPublishType())) {
                // 考试组
                if (ExamFormEnums.FORM_EXAM_GROUP.getCode().equals(examInfo.getExamForm()) && CommonEnums.EXAM_MERGE_TYPE_0.getCode().equals(String.valueOf(examInfo.getMergeType()))) {
                    examResultVo.setExamInfoVos(buildExamAndSubjectPublishExamnfoVos(examInfo.getId(), examPublishInfo.getId()));
                    examPublishVo.setExamResultVo(examResultVo);
                } else {
                    examResultVo.setSubjectInfoVos(bulidSubjectInfoVos(examInfo.getId(), examPublishInfo.getId()));
                    examPublishVo.setExamResultVo(examResultVo);
                }
            }
            if (PublishTypeEnums.PUBLISH_TYPE_1.getCode().equals(examPublishInfo.getPublishType())) {
                // 发布的试卷
                LambdaQueryWrapper<ExamineeExamPaperInfo> gradeWrapper = Wrappers.lambdaQuery();
                gradeWrapper.eq(ExamineeExamPaperInfo::getExamPublishId, examPublishInfo.getId());
                List<ExamineeExamPaperInfo> examineeExamPaperInfos = examineeExamPaperInfoMapper.selectList(gradeWrapper);
                // 考试组(合并考试)
                if (ExamFormEnums.FORM_EXAM_GROUP.getCode().equals(examInfo.getExamForm()) && CommonEnums.EXAM_MERGE_TYPE_0.getCode().equals(String.valueOf(examInfo.getMergeType()))) {
                    examResultVo.setEaxmPublishPaperVOs(bulidEaxmPublishPaperVOs(examInfo.getId(), examineeExamPaperInfos));

                } else {
                    examResultVo.setEaxmPublishPaperVOs(buildEaxmPublishPaperVOs(examInfo, examineeExamPaperInfos));

                }
                // 处理前端显示
                List<PaperPublishVO> paperPublishVOs = new ArrayList<>();
                for (EaxmPublishPaperVO eaxmPublishPaperVO : examResultVo.getEaxmPublishPaperVOs()) {
                    if (CollectionUtils.isNotEmpty(eaxmPublishPaperVO.getPaperVOs())) {
                        paperPublishVOs.addAll(eaxmPublishPaperVO.getPaperVOs());
                    }
                }
                if (CollectionUtils.isEmpty(paperPublishVOs)) {
                    examResultVo.setEaxmPublishPaperVOs(Collections.EMPTY_LIST);
                }
                examPublishVo.setExamPaperVo(examResultVo);

            }
            if (PublishTypeEnums.PUBLISH_TYPE_2.getCode().equals(examPublishInfo.getPublishType())) {
                examPublishVo.setExamReviewVo(examResultVo);
            }
            if (PublishTypeEnums.ZS_DOWNLOAD_TYPE_3.getCode().equals(examPublishInfo.getPublishType())) {
                // 查询考试是否关联证书
                ZsInfoVO zsInfoVO = zsInfoService.getZsInfoByPublishTypeToExam(examInfo.getId());
                if (null == zsInfoVO) {
                    examResultVo.setRelationZs(Boolean.FALSE);
                    examPublishVo.setExamPublishZsInfoVo(examResultVo);
                } else {
                    // 是否关联证书
                    examResultVo.setRelationZs(Boolean.TRUE);

                    List<ExamPublishCertificateInfo> examPublishCertificateInfos = examPublishCertificateInfoService.listByExamId(examResultVo.getExamId());
                    // Map<证书ID，考试发布绑定信息>
                    Map<String, ExamPublishCertificateInfo> zsInfoIdMappingCertificateInfo = examPublishCertificateInfos.stream()
                            .collect(Collectors.toMap(ExamPublishCertificateInfo::getZsInfoId,
                                    data -> data,
                                    (before, after) -> before));

                    List<ZsInfo> zsInfos = zsInfoService.listByExamId(examResultVo.getExamId());
                    List<ExamPublishCertificateInfoVO> certificateInfoVOS = zsInfos.stream()
                            .map(this::assemblePublishCertificateInfoVO)
                            .peek(data -> {
                                // 获取证书对应的成绩发布证书绑定数据
                                ExamPublishCertificateInfo examPublishCertificateInfo = MapUtil.get(zsInfoIdMappingCertificateInfo, data.getZsInfoId(), ExamPublishCertificateInfo.class);
                                if (examPublishCertificateInfo != null) {
                                    BeanUtil.copyProperties(examPublishCertificateInfo, data);
                                }
                            }).collect(Collectors.toList());
                    examResultVo.setExamPublishCertificateInfoVOS(certificateInfoVOS);

                    examPublishVo.setExamPublishZsInfoVo(examResultVo);
                }
            }
        }
        return examPublishVo;
    }

    /**
     * 装配发布证书信息VO
     *
     * @param zsInfo
     * @return
     */
    private ExamPublishCertificateInfoVO assemblePublishCertificateInfoVO(ZsInfo zsInfo) {
        ExamPublishCertificateInfoVO publishCertificateInfoVO = BeanUtil.toBean(zsInfo, ExamPublishCertificateInfoVO.class);
        publishCertificateInfoVO.setZsInfoId(zsInfo.getId());
        return publishCertificateInfoVO;
    }

    /**
     * 普通考试发布勾选试卷
     *
     * @param examInfo
     * @param examineeExamPaperInfos
     * @return
     */
    private List<EaxmPublishPaperVO> buildEaxmPublishPaperVOs(ExamInfo examInfo, List<ExamineeExamPaperInfo> examineeExamPaperInfos) {
        List<EaxmPublishPaperVO> eaxmPublishPaperVOs = new ArrayList<>();
        ExamNameConfig examNameConfig = examNameConfigMapper.selectExamNameConfigById(examInfo.getExamNameConfigId());

        Map<String, ExamineeExamPaperInfo> examPaperInfoMap = examineeExamPaperInfos.stream().collect(Collectors.toMap(ExamineeExamPaperInfo::getPaperId, ExamineeExamPaperInfo -> ExamineeExamPaperInfo, (key1, key2) -> key2));

        QueryWrapper<ExamineeExamInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ExamineeExamInfo::getExamId, examInfo.getId()).groupBy(ExamineeExamInfo::getPaperId);
        List<ExamineeExamInfo> examineeExamInfos = examineeExamInfoMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(examineeExamInfos)) {
            return Collections.EMPTY_LIST;
        }
        //A、B卷处理
        if (examineeExamInfos.size() > 1) {
            for (ExamineeExamInfo examineeExamInfo : examineeExamInfos) {
                if (StringUtils.isEmpty(examineeExamInfo.getExamineeExamInfo())) {
                    return Collections.EMPTY_LIST;
                }
                CandidateAnswersVO candidateAnswersVO = JSON.parseObject(examineeExamInfo.getExamineeExamInfo(), CandidateAnswersVO.class);
                if (CollectionUtils.isEmpty(candidateAnswersVO.getCandidatePaperDataList())) {
                    return Collections.EMPTY_LIST;
                }

                EaxmPublishPaperVO paperVO = new EaxmPublishPaperVO();
                paperVO.setExamId(examInfo.getId());
                paperVO.setExamName(examNameConfig.getName());
                List<PaperPublishVO> paperVOs = new ArrayList<>(candidateAnswersVO.getCandidatePaperDataList().size());
                for (CandidatePaperDataVO paperDataVO : candidateAnswersVO.getCandidatePaperDataList()) {
                    PaperPublishVO paper = new PaperPublishVO();
                    paper.setPaperId(paperDataVO.getPaperId());
                    paper.setPaperName(paperDataVO.getPaperName());
                    paper.setShowPaperResult(Integer.valueOf(CommonEnums.CHECK_STATUS_0.getCode()));
                    if (MapUtils.isNotEmpty(examPaperInfoMap) && null != examPaperInfoMap.get(paperDataVO.getPaperId())) {
                        paper.setShowPaperResult(examPaperInfoMap.get(paperDataVO.getPaperId()).getShowPaperResult());
                    }
                    paperVOs.add(paper);
                }
                paperVO.setPaperVOs(paperVOs);
                eaxmPublishPaperVOs.add(paperVO);
            }
        } else {
            if (StringUtils.isEmpty(examineeExamInfos.get(0).getExamineeExamInfo())) {
                return Collections.EMPTY_LIST;
            }
            CandidateAnswersVO candidateAnswersVO = JSON.parseObject(examineeExamInfos.get(0).getExamineeExamInfo(), CandidateAnswersVO.class);
            if (CollectionUtils.isEmpty(candidateAnswersVO.getCandidatePaperDataList())) {
                return Collections.EMPTY_LIST;
            }

            EaxmPublishPaperVO paperVO = new EaxmPublishPaperVO();
            paperVO.setExamId(examInfo.getId());
            paperVO.setExamName(examNameConfig.getName());
            List<PaperPublishVO> paperVOs = new ArrayList<>(candidateAnswersVO.getCandidatePaperDataList().size());
            for (CandidatePaperDataVO paperDataVO : candidateAnswersVO.getCandidatePaperDataList()) {
                PaperPublishVO paper = new PaperPublishVO();
                paper.setPaperId(paperDataVO.getPaperId());
                paper.setPaperName(paperDataVO.getPaperName());
                paper.setShowPaperResult(Integer.valueOf(CommonEnums.CHECK_STATUS_0.getCode()));
                if (MapUtils.isNotEmpty(examPaperInfoMap) && null != examPaperInfoMap.get(paperDataVO.getPaperId())) {
                    paper.setShowPaperResult(examPaperInfoMap.get(paperDataVO.getPaperId()).getShowPaperResult());
                }
                paperVOs.add(paper);
            }
            paperVO.setPaperVOs(paperVOs);
            eaxmPublishPaperVOs.add(paperVO);


        }

        return eaxmPublishPaperVOs;
    }


    /**
     * 考试组发布勾选试卷
     *
     * @param examId
     * @param examineeExamPaperInfos
     * @return
     */
    private List<EaxmPublishPaperVO> bulidEaxmPublishPaperVOs(String examId, List<ExamineeExamPaperInfo> examineeExamPaperInfos) {
        //查询子考试
        List<ExamInfoVo> examInfoVos = getExamInfoVos(examId);
        Map<String, List<ExamineeExamPaperInfo>> examineeExamPaperInfoMap = examineeExamPaperInfos.stream().collect(Collectors.groupingBy(c -> c.getExamId()));

        if (CollectionUtils.isEmpty(examInfoVos)) {
            return Collections.emptyList();
        }
        List<EaxmPublishPaperVO> eaxmPublishPaperVOs = new ArrayList<>(examInfoVos.size());
        for (ExamInfoVo examInfoVo : examInfoVos) {
            QueryWrapper<ExamineeExamInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(ExamineeExamInfo::getExamId, examInfoVo.getId()).groupBy(ExamineeExamInfo::getPaperId);
            List<ExamineeExamInfo> examineeExamInfos = examineeExamInfoMapper.selectList(queryWrapper);
            if (CollectionUtils.isEmpty(examineeExamInfos)) {
                continue;
            }
            //A、B卷处理
            if (examineeExamInfos.size() > 1) {
                for (ExamineeExamInfo examineeExamInfo : examineeExamInfos) {
                    if (StringUtils.isEmpty(examineeExamInfo.getExamineeExamInfo())) {
                        continue;
                    }
                    CandidateAnswersVO candidateAnswersVO = JSON.parseObject(examineeExamInfo.getExamineeExamInfo(), CandidateAnswersVO.class);
                    if (CollectionUtils.isEmpty(candidateAnswersVO.getCandidatePaperDataList())) {
                        continue;
                    }
                    List<ExamineeExamPaperInfo> paperInfos = examineeExamPaperInfoMap.get(examInfoVo.getId());
                    Map<String, ExamineeExamPaperInfo> examPaperInfoMap = paperInfos.stream().collect(Collectors.toMap(ExamineeExamPaperInfo::getPaperId, ExamineeExamPaperInfo -> ExamineeExamPaperInfo, (key1, key2) -> key2));
                    EaxmPublishPaperVO paperVO = new EaxmPublishPaperVO();
                    paperVO.setExamId(examInfoVo.getId());
                    paperVO.setExamName(examInfoVo.getExamNameConfigName());
                    List<PaperPublishVO> paperVOs = new ArrayList<>(examInfoVos.size());
                    for (CandidatePaperDataVO candidatePaperDataVO : candidateAnswersVO.getCandidatePaperDataList()) {
                        PaperPublishVO paper = new PaperPublishVO();
                        paper.setShowPaperResult(Integer.valueOf(CommonEnums.CHECK_STATUS_0.getCode()));
                        paper.setPaperId(candidatePaperDataVO.getPaperId());
                        paper.setPaperName(candidatePaperDataVO.getPaperName());
                        if (MapUtils.isNotEmpty(examPaperInfoMap) && null != examPaperInfoMap.get(candidatePaperDataVO.getPaperId())) {
                            paper.setShowPaperResult(examPaperInfoMap.get(candidatePaperDataVO.getPaperId()).getShowPaperResult());
                        }
                        paperVOs.add(paper);
                    }
                    paperVO.setPaperVOs(paperVOs);
                    eaxmPublishPaperVOs.add(paperVO);
                }

            } else {
                if (StringUtils.isEmpty(examineeExamInfos.get(0).getExamineeExamInfo())) {
                    continue;
                }
                CandidateAnswersVO candidateAnswersVO = JSON.parseObject(examineeExamInfos.get(0).getExamineeExamInfo(), CandidateAnswersVO.class);
                if (CollectionUtils.isEmpty(candidateAnswersVO.getCandidatePaperDataList())) {
                    continue;
                }
                List<ExamineeExamPaperInfo> paperInfos = examineeExamPaperInfoMap.get(examInfoVo.getId());
                Map<String, ExamineeExamPaperInfo> examPaperInfoMap = paperInfos.stream().collect(Collectors.toMap(ExamineeExamPaperInfo::getPaperId, ExamineeExamPaperInfo -> ExamineeExamPaperInfo, (key1, key2) -> key2));
                EaxmPublishPaperVO paperVO = new EaxmPublishPaperVO();
                paperVO.setExamId(examInfoVo.getId());
                paperVO.setExamName(examInfoVo.getExamNameConfigName());
                List<PaperPublishVO> paperVOs = new ArrayList<>(examInfoVos.size());
                for (CandidatePaperDataVO candidatePaperDataVO : candidateAnswersVO.getCandidatePaperDataList()) {
                    PaperPublishVO paper = new PaperPublishVO();
                    paper.setShowPaperResult(Integer.valueOf(CommonEnums.CHECK_STATUS_0.getCode()));
                    paper.setPaperId(candidatePaperDataVO.getPaperId());
                    paper.setPaperName(candidatePaperDataVO.getPaperName());
                    if (MapUtils.isNotEmpty(examPaperInfoMap) && null != examPaperInfoMap.get(candidatePaperDataVO.getPaperId())) {
                        paper.setShowPaperResult(examPaperInfoMap.get(candidatePaperDataVO.getPaperId()).getShowPaperResult());
                    }
                    paperVOs.add(paper);
                }
                paperVO.setPaperVOs(paperVOs);
                eaxmPublishPaperVOs.add(paperVO);
            }


        }
        return eaxmPublishPaperVOs;
    }

    /**
     * 只发布科目显示
     *
     * @param examId
     * @param examPublishInfoId
     * @return
     */
    private List<ExamPublishSubjectInfoVo> bulidSubjectInfoVos(String examId, String examPublishInfoId) {
        //查考试下的科目
        List<ExamSubjectVo> examSubjectVos = examSubjectInfoMapper.selecSubjectList(examId);
        if (CollectionUtils.isEmpty(examSubjectVos)) {
            return Collections.EMPTY_LIST;
        }
        LambdaQueryWrapper<ExamPublishSubjectInfo> gradeWrapper = Wrappers.lambdaQuery();
        gradeWrapper.eq(ExamPublishSubjectInfo::getExamPublishId, examPublishInfoId);
        List<ExamPublishSubjectInfo> examPublishSubjectInfos = examPublishSubjectInfoMapper.selectList(gradeWrapper);

        //设置显示考试or科目信息
        List<ExamPublishSubjectInfoVo> subjectInfoVos = new ArrayList<>(examSubjectVos.size());
        Map<String, ExamPublishSubjectInfo> examPublishSubjectMap = new HashMap<>(examSubjectVos.size());
        if (CollectionUtils.isNotEmpty(examPublishSubjectInfos)) {
            examPublishSubjectMap = examPublishSubjectInfos.stream().collect(Collectors.toMap(ExamPublishSubjectInfo::getSubjectId, ExamPublishSubjectInfo -> ExamPublishSubjectInfo, (key1, key2) -> key2));
        }
        for (ExamSubjectVo examSubjectVo : examSubjectVos) {
            ExamPublishSubjectInfoVo vo = new ExamPublishSubjectInfoVo();
            if (examPublishSubjectMap.get(examSubjectVo.getId()) == null) {
                vo.setSubjectId(examSubjectVo.getId());
                vo.setShowSubjectResult(CommonConstant.ZERO);
                vo.setShowSubjectResult(CommonConstant.ZERO);
            } else {
                BeanUtils.copyProperties(examPublishSubjectMap.get(examSubjectVo.getId()), vo);
            }
            vo.setSubjectName(examSubjectVo.getSubjectName());
            subjectInfoVos.add(vo);
        }
        return subjectInfoVos;
    }


    /**
     * 考试组发布考试和科目
     *
     * @param examId
     * @param examPublishInfoId
     * @return
     */
    private List<ExamPublishExamnfoVo> buildExamAndSubjectPublishExamnfoVos(String examId, String examPublishInfoId) {
        //查询子考试
        List<ExamInfoVo> examInfoVos = getExamInfoVos(examId);
        if (CollectionUtils.isEmpty(examInfoVos)) {
            return Collections.EMPTY_LIST;
        }
        //发布的考试信息
        LambdaQueryWrapper<ExamPublishSubjectInfo> gradeWrapper = Wrappers.lambdaQuery();
        gradeWrapper.eq(ExamPublishSubjectInfo::getExamPublishId, examPublishInfoId);
        List<ExamPublishSubjectInfo> examPublishSubjectInfos = examPublishSubjectInfoMapper.selectList(gradeWrapper);
        Map<String, List<ExamPublishSubjectInfo>> examPublishSubjectMap = new HashMap<>(examInfoVos.size());
        List<ExamPublishExamnfoVo> examPublishInfoVos = new ArrayList<>(examInfoVos.size());

        if (CollectionUtils.isNotEmpty(examPublishSubjectInfos)) {
            examPublishSubjectMap = examPublishSubjectInfos.stream().collect(Collectors.groupingBy(c -> c.getRelationExamId()));
        }
        //查考试下的科目
        Set<String> eaxmIds = examInfoVos.stream().map(e -> e.getId()).collect(Collectors.toSet());
        List<ExamSubjectVo> examSubjectVos = examSubjectInfoMapper.selectExamSubjectListByExamIds(eaxmIds.toArray(new String[eaxmIds.size()]));
        Map<String, String> subjectNameMap = null;
        if (CollectionUtils.isNotEmpty(examSubjectVos)) {
            subjectNameMap = examSubjectVos.stream().collect(Collectors.toMap(ExamSubjectVo::getSubjectId, ExamSubjectVo::getSubjectName, (key1, key2) -> key2));
        }

        for (ExamInfoVo examInfoVo : examInfoVos) {
            ExamPublishExamnfoVo publishExamnfoVo = new ExamPublishExamnfoVo();
            List<ExamPublishSubjectInfo> examInfos = examPublishSubjectMap.get(examInfoVo.getId());
            if (CollectionUtils.isEmpty(examInfos)) {
                continue;
            }
            List<ExamPublishSubjectInfoVo> examPublishSubjectInfoVos = new ArrayList<>(examInfos.size());
            publishExamnfoVo.setExamPublishId(examInfos.get(0).getExamPublishId());
            publishExamnfoVo.setRelationExamId(examInfos.get(0).getRelationExamId());
            publishExamnfoVo.setRelationExamName(examInfoVo.getExamNameConfigName());
            publishExamnfoVo.setShowExamScore(examInfos.get(0).getShowExamScore());
            publishExamnfoVo.setShowExamResult(examInfos.get(0).getShowExamResult());

            for (ExamPublishSubjectInfo info : examInfos) {
                ExamPublishSubjectInfoVo vo = new ExamPublishSubjectInfoVo();
                vo.setSubjectId(info.getSubjectId());
                vo.setShowSubjectScore(info.getShowSubjectScore());
                vo.setShowSubjectResult(info.getShowSubjectResult());
                vo.setExamPublishId(info.getExamPublishId());
                if (MapUtils.isNotEmpty(subjectNameMap) && StringUtils.isNotEmpty(subjectNameMap.get(info.getSubjectId()))) {
                    vo.setSubjectName(subjectNameMap.get(info.getSubjectId()));
                }
                examPublishSubjectInfoVos.add(vo);
            }
            publishExamnfoVo.setExamPublishSubjectInfoVos(examPublishSubjectInfoVos);
            examPublishInfoVos.add(publishExamnfoVo);
        }
        return examPublishInfoVos;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveExamPublishInfoVo(ExamPublishVo vo) {
        if (StringUtils.isEmpty(vo.getExamId())) {
            return;
        }
        QueryWrapper<ExamPublishInfo> examQueryWrapper = new QueryWrapper();
        examQueryWrapper.lambda().eq(ExamPublishInfo::getExamId, vo.getExamId());
        List<ExamPublishInfo> examPublishInfos = examPublishInfoMapper.selectList(examQueryWrapper);
        //先删除
        if (CollectionUtils.isNotEmpty(examPublishInfos)) {
            for (ExamPublishInfo examPublishInfo : examPublishInfos) {
                examPublishInfoMapper.deleteById(examPublishInfo.getId());
                QueryWrapper<ExamPublishSubjectInfo> queryWrapper = new QueryWrapper();
                queryWrapper.lambda().eq(ExamPublishSubjectInfo::getExamPublishId, examPublishInfo.getId());
                examPublishSubjectInfoMapper.delete(queryWrapper);
                QueryWrapper<ExamineeExamPaperInfo> queryPaperWrapper = new QueryWrapper();
                queryPaperWrapper.lambda().eq(ExamineeExamPaperInfo::getExamPublishId, examPublishInfo.getId());
                examineeExamPaperInfoMapper.delete(queryPaperWrapper);
                // 发布证书信息删除
                examPublishCertificateInfoService.lambdaUpdate()
                        .eq(ExamPublishCertificateInfo::getExamPublishId, examPublishInfo.getId())
                        .remove();
            }
        }
        List<ExamPublishInfoVo> examPublishInfoVos = new ArrayList<>(3);
        examPublishInfoVos.add(vo.getExamResultVo());
        examPublishInfoVos.add(vo.getExamPaperVo());
        examPublishInfoVos.add(vo.getExamReviewVo());
        examPublishInfoVos.add(vo.getExamPublishZsInfoVo());
        for (ExamPublishInfoVo examPublishInfoVo : examPublishInfoVos) {
            if (null == examPublishInfoVo) {
                continue;
            }
            if (PublishTypeEnums.PUBLISH_TYPE_0.getCode().equals(examPublishInfoVo.getPublishType())) {
                String message = checkPublishType0(examPublishInfoVo);
                if (!Constants.SUCCESS.equals(message)) {
                    throw new CustomException(message);
                }
            }

            if (Integer.valueOf(CommonEnums.SEE_TYPE_1.getCode()).equals(examPublishInfoVo.getSeeType())) {
                String message = checkPublishType2(examPublishInfoVo);
                if (!Constants.SUCCESS.equals(message)) {
                    throw new CustomException(message);
                }
            }
            User user = iBaseLoginService.getSysUser();


            ExamPublishInfo examPublishInfo = new ExamPublishInfo();
            BeanUtils.copyProperties(examPublishInfoVo, examPublishInfo);
            examPublishInfo.createInfo(user.getLoginname());
            examPublishInfo.updateInfo(user.getLoginname());
            examPublishInfo.setShowType("");
            if (CollectionUtils.isNotEmpty(examPublishInfoVo.getShowTypes())) {
                examPublishInfo.setShowType(String.join(",", examPublishInfoVo.getShowTypes()));
            }
            examPublishInfo.setId(UUIDUtils.newSortUUID());
            examPublishInfoMapper.insert(examPublishInfo);

            //科目
            if (CollectionUtils.isNotEmpty(examPublishInfoVo.getSubjectInfoVos())) {
                for (ExamPublishSubjectInfoVo examPublishSubjectInfoVo : examPublishInfoVo.getSubjectInfoVos()) {
                    if (null == examPublishSubjectInfoVo) {
                        continue;
                    }
                    ExamPublishSubjectInfo examPublishSubjectInfo = new ExamPublishSubjectInfo();
                    BeanUtils.copyProperties(examPublishSubjectInfoVo, examPublishSubjectInfo);
                    examPublishSubjectInfo.createInfo(user.getLoginname());
                    examPublishSubjectInfo.updateInfo(user.getLoginname());
                    examPublishSubjectInfo.setId(UUIDUtils.newSortUUID());
                    examPublishSubjectInfo.setExamPublishId(examPublishInfo.getId());
                    examPublishSubjectInfoMapper.insert(examPublishSubjectInfo);

                }
            }
            //子考试
            if (CollectionUtils.isNotEmpty(examPublishInfoVo.getExamInfoVos())) {
                for (ExamPublishExamnfoVo examPublishExamnfoVo : examPublishInfoVo.getExamInfoVos()) {
                    if (null == examPublishExamnfoVo) {
                        continue;
                    }
                    ExamPublishSubjectInfo examPublishExamnfo = new ExamPublishSubjectInfo();
                    for (ExamPublishSubjectInfoVo examPublishSubjectInfoVo : examPublishExamnfoVo.getExamPublishSubjectInfoVos()) {
                        examPublishExamnfo.setExamPublishId(examPublishInfo.getId());
                        examPublishExamnfo.setRelationExamId(examPublishExamnfoVo.getRelationExamId());
                        examPublishExamnfo.setShowExamResult(examPublishExamnfoVo.getShowExamResult());
                        examPublishExamnfo.setShowExamScore(examPublishExamnfoVo.getShowExamScore());
                        examPublishExamnfo.setSubjectId(examPublishSubjectInfoVo.getSubjectId());
                        examPublishExamnfo.setShowSubjectResult(examPublishSubjectInfoVo.getShowSubjectResult());
                        examPublishExamnfo.setShowSubjectScore(examPublishSubjectInfoVo.getShowSubjectScore());
                        examPublishExamnfo.setId(UUIDUtils.newSortUUID());
                        examPublishSubjectInfoMapper.insert(examPublishExamnfo);
                    }
                }
            }

            //试卷
            if (CollectionUtils.isNotEmpty(examPublishInfoVo.getEaxmPublishPaperVOs())) {
                for (EaxmPublishPaperVO eaxmPublishPaperVO : examPublishInfoVo.getEaxmPublishPaperVOs()) {
                    if (null == eaxmPublishPaperVO || CollectionUtils.isEmpty(eaxmPublishPaperVO.getPaperVOs())) {
                        continue;
                    }
                    for (PaperPublishVO paperPublishVO : eaxmPublishPaperVO.getPaperVOs()) {
                        ExamineeExamPaperInfo examineeExamPaperInfo = new ExamineeExamPaperInfo();
                        examineeExamPaperInfo.setExamId(eaxmPublishPaperVO.getExamId());
                        examineeExamPaperInfo.setPaperId(paperPublishVO.getPaperId());
                        examineeExamPaperInfo.setPaperName(paperPublishVO.getPaperName());
                        examineeExamPaperInfo.setShowPaperResult(paperPublishVO.getShowPaperResult());
                        examineeExamPaperInfo.createInfo(user.getLoginname());
                        examineeExamPaperInfo.updateInfo(user.getLoginname());
                        examineeExamPaperInfo.setId(UUIDUtils.newSortUUID());
                        examineeExamPaperInfo.setExamPublishId(examPublishInfo.getId());
                        examineeExamPaperInfoMapper.insert(examineeExamPaperInfo);
                    }
                }
            }

            // 证书
            if (CollectionUtils.isNotEmpty(examPublishInfoVo.getExamPublishCertificateInfoVOS())) {
                for (ExamPublishCertificateInfoVO examPublishCertificateInfoVO : examPublishInfoVo.getExamPublishCertificateInfoVOS()) {
                    ExamPublishCertificateInfo examPublishCertificateInfo = BeanUtil.toBean(examPublishCertificateInfoVO, ExamPublishCertificateInfo.class);
                    examPublishCertificateInfo.setExamInfoId(examPublishInfoVo.getExamId());
                    examPublishCertificateInfo.setCreateBy(user.getLoginname());
                    examPublishCertificateInfo.setUpdateBy(user.getLoginname());
                    examPublishCertificateInfo.setCreateTime(LocalDateTime.now());
                    examPublishCertificateInfo.setUpdateTime(LocalDateTime.now());
                    examPublishCertificateInfo.setId(UUIDUtils.newSortUUID());
                    examPublishCertificateInfo.setExamPublishId(examPublishInfo.getId());
                    examPublishCertificateInfoService.save(examPublishCertificateInfo);
                }

            }
        }
    }

    private String checkPublishType2(ExamPublishInfoVo examPublishInfoVo) {
        String desc = PublishTypeEnums.getAnalysisByCode(examPublishInfoVo.getPublishType()).getDesc();
        if (null == examPublishInfoVo.getStartDay()) {
            throw new CustomException(desc + "查看开始时间不能设置为空！");
        }
        if (null == examPublishInfoVo.getEndDay()) {
            throw new CustomException(desc + "查看结束时间不能设置为空！");
        }
        if (examPublishInfoVo.getStartDay().after(examPublishInfoVo.getEndDay())) {
            throw new CustomException(desc + "开始时间必须小于结束时间！");
        }
        if (examPublishInfoVo.getEndDay().before(DateUtils.getNowDate())) {
            throw new CustomException(desc + "结束时间必须大于当前时间！");
        }
        return Constants.SUCCESS;
    }


    private String checkPublishType0(ExamPublishInfoVo examPublishInfoVo) {
        ExamInfo examInfo = examInfoMapper.selectExamInfoById(examPublishInfoVo.getExamId());
        if (null == examInfo) {
            throw new CustomException("发布的考试不存在！");
        }
        List<ScoreLineItem> scoreLineItems = enterpriseExamScoreLineSettingSubMapper.selectEnterpriseExamScoreLineSettingByExamId(examPublishInfoVo.getExamId());
        if (CollectionUtils.isNotEmpty(examPublishInfoVo.getShowTypes()) && examPublishInfoVo.getShowTypes().contains(ShowTypeEnums.SHOW_TYPE_1.getCode())) {
            if (CollectionUtils.isEmpty(scoreLineItems)) {
                return "本场考试未设置分数线，不允许发布考试结果！";
            }
            Map<String, List<ScoreLineItem>> scoreLineMap = scoreLineItems.stream().filter(s -> StringUtils.isNotEmpty(s.getDimension())).collect(Collectors.groupingBy(c -> c.getDimension()));
            //考试组
            if (ExamFormEnums.FORM_EXAM_GROUP.getCode().equals(examInfo.getExamForm()) &&
                    CommonEnums.EXAM_MERGE_TYPE_0.getCode().equals(String.valueOf(examInfo.getMergeType()))) {
                if (CollectionUtils.isEmpty(scoreLineMap.get(CommonEnums.SCORE_LINE_DIMENSION_2.getCode()))) {
                    return "本场考试未设置分数线，不允许发布考试结果！";
                }
            } else {
                if (CollectionUtils.isEmpty(scoreLineMap.get(CommonEnums.SCORE_LINE_DIMENSION_0.getCode()))) {
                    return "本场考试未设置分数线，不允许发布考试结果！";
                }
            }

        }
        if (CollectionUtils.isNotEmpty(examPublishInfoVo.getShowTypes()) &&
                examPublishInfoVo.getShowTypes().contains(ShowTypeEnums.SHOW_TYPE_2.getCode()) &&
                CollectionUtils.isNotEmpty(examPublishInfoVo.getSubjectInfoVos())) {
            //选中的科目在设置分数线的科目里面不存在
            for (ExamPublishSubjectInfoVo examPublishSubjectInfoVo : examPublishInfoVo.getSubjectInfoVos()) {
                if (Integer.valueOf(CommonEnums.CHECK_STATUS_1.getCode()).equals(examPublishSubjectInfoVo.getShowSubjectResult())) {
                    if (CollectionUtils.isEmpty(scoreLineItems)) {
                        return examPublishSubjectInfoVo.getSubjectName() + "科目未设置分数线，不允许发布显示科目考试结果！";
                    }
                    Map<String, List<ScoreLineItem>> scoreLineMap = scoreLineItems.stream().collect(Collectors.groupingBy(c -> c.getDimension()));
                    if (CollectionUtils.isEmpty(scoreLineMap.get(CommonEnums.SCORE_LINE_DIMENSION_1.getCode()))) {
                        return examPublishSubjectInfoVo.getSubjectName() + "科目未设置分数线，不允许发布显示科目考试结果！";
                    }
                    Map<String, String> subjectMap = scoreLineMap.get(CommonEnums.SCORE_LINE_DIMENSION_1.getCode()).stream().
                            collect(Collectors.toMap(ScoreLineItem::getSubjectId, ScoreLineItem::getSubjectName, (key1, key2) -> key2));
                    if (MapUtils.isEmpty(subjectMap) || StringUtils.isEmpty(subjectMap.get(examPublishSubjectInfoVo.getSubjectId()))) {
                        return examPublishSubjectInfoVo.getSubjectName() + "科目未设置分数线，不允许发布显示科目考试结果！";
                    }
                }
            }
        }


        //考试组
        if (CollectionUtils.isNotEmpty(examPublishInfoVo.getShowTypes()) &&
                examPublishInfoVo.getShowTypes().contains(ShowTypeEnums.SHOW_TYPE_5.getCode()) &&
                CollectionUtils.isNotEmpty(examPublishInfoVo.getExamInfoVos())) {
            //选中的科目在设置分数线的科目里面不存在
            for (ExamPublishExamnfoVo examPublishExamnfoVo : examPublishInfoVo.getExamInfoVos()) {
                scoreLineItems = enterpriseExamScoreLineSettingSubMapper.selectEnterpriseExamScoreLineSettingByExamId(examPublishExamnfoVo.getRelationExamId());

                if (Integer.valueOf(CommonEnums.CHECK_STATUS_1.getCode()).equals(examPublishExamnfoVo.getShowExamResult())) {
                    if (CollectionUtils.isEmpty(scoreLineItems)) {
                        return examPublishExamnfoVo.getRelationExamName() + "考试未设置分数线，不允许发布显示科目考试结果！";
                    }
                    Map<String, List<ScoreLineItem>> scoreLineMap = scoreLineItems.stream().collect(Collectors.groupingBy(c -> c.getDimension()));
                    if (CollectionUtils.isEmpty(scoreLineMap.get(CommonEnums.SCORE_LINE_DIMENSION_1.getCode()))) {
                        return examPublishExamnfoVo.getRelationExamName() + "考试未设置分数线，不允许发布显示科目考试结果！";
                    }
                    Map<String, String> subjectMap = scoreLineMap.get(CommonEnums.SCORE_LINE_DIMENSION_1.getCode()).stream().
                            collect(Collectors.toMap(ScoreLineItem::getSubjectId, ScoreLineItem::getSubjectName, (key1, key2) -> key2));
                    boolean flag = false;
                    if (MapUtils.isEmpty(subjectMap)) {
                        flag = true;
                    }
                    //查询科目是否设置分数线
                    for (ExamPublishSubjectInfoVo examPublishSubjectInfoVo : examPublishExamnfoVo.getExamPublishSubjectInfoVos()) {
                        flag = true;
                        if (StringUtils.isNotEmpty(subjectMap.get(examPublishSubjectInfoVo.getSubjectId()))) {
                            flag = false;
                            break;
                        }
                    }
                    if (flag) {
                        return examPublishExamnfoVo.getRelationExamName() + "考试未设置分数线，不允许发布显示科目考试结果！";
                    }
                }
            }
        }
        return Constants.SUCCESS;
    }


    private List<ExamInfoVo> getExamInfoVos(String examId) {
        List<ExamInfoVo> examInfoVos = null;
        ExamInfo examInfo = examInfoMapper.selectExamInfoById(examId);
        if (null != examInfo && null != examInfo.getMergeType() &&
                CommonEnums.EXAM_MERGE_TYPE_0.getCode().equals(String.valueOf(examInfo.getMergeType()))) {
            Map<String, Object> parms = new HashMap<>();
            parms.put("relationId", examId);
            examInfoVos = examInfoMapper.selectEnterpriseExamInfoByParms(parms);
        }

        return examInfoVos;
    }
}
